package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.PageResult;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        //清理某个分类下面的菜品缓存数据
        String key = "dish_" + dishDto.getCategoryId() + "_1";
        redisTemplate.delete(key);

        return R.success(null,"新增菜品成功");
    }

    @GetMapping("/page")
    public R<PageResult> findPage(@RequestParam(required = false, defaultValue = "1") Long page, @RequestParam(name = "pageSize", defaultValue = "1") Long pageSize,String name){
        return R.success(dishService.findPage(page,pageSize,name));
    }

    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        //清理某个分类下面的菜品缓存数据
        String key = "dish_" + dishDto.getCategoryId() + "_1";
        redisTemplate.delete(key);
        return R.success("更新菜品成功");
    }

//    @PostMapping("/status")
//    public R<String> uploadStatus(HttpSession session, @RequestBody Dish dish){
//        long id = Thread.currentThread().getId();
//        log.debug("*****************EmployeeController************线程id为{}**",id);
//        return dishService.uploadStatus(session,dish);
//    }
    /**
     * 停售
     * @param
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> uploadStatus(@PathVariable("status") Integer status, @RequestParam List<Long> ids){
        return dishService.uploadStatus(status,ids);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.deleteWithDish(ids);
        return R.success(null,"删除套餐成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtoList = dishService.list(dish);
        return R.success(dishDtoList);
    }
}
